package com.rchat.pocmini.activity.searchgroup;

import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SearchView;

import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.adapter.SearchGroupResultListViewAdapter;
import com.rchat.pocmini.bean.SearchGroupBean;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ljd on 2017/8/2.
 *
 */

public class SearchGroupMvpActivity extends BaseMvpActivity<SearchGroupPresenterImpl, SearchGroupModelImpl>
        implements SearchGroupContract.View{

    @BindView(R.id.sv_search_group)
    SearchView mSearchView;

    @BindView(R.id.lv_search_group_result)
    ListView mLvSearchGroupResult;

    @BindView(R.id.iv_back)
    ImageView mIvBack;

    @BindView(R.id.pb)
    ProgressBar mPb;

    private ArrayList<SearchGroupBean.DataBean.GroupsBean.GroupBean> searchResultList;
    private SearchGroupResultListViewAdapter adapter;


    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public int getLayoutId() {
        return R.layout.search_group_activity;
    }

    @Override
    public void initView() {
        searchResultList = new ArrayList<>();
        adapter = new SearchGroupResultListViewAdapter(this,searchResultList);
        mLvSearchGroupResult.setAdapter(adapter);

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String groupName) {
                mPresenter.searchGroup(groupName);
                return false;
            }
            // 当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    @Override
    public void doAfterInitView() {

    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void notifyDataChanged(SearchGroupBean searchGroupBean) {
        searchResultList.clear();
        searchResultList.addAll(searchGroupBean.getData().getGroups().getGroup());
        adapter.notifyDataSetChanged();
    }
}
